<?php

class lib_Form
{
	protected $_options = array('name'					=>  '',
								'targetPath'            =>  '../application/forms',
								'parserPath'            =>  'Parser',
								'suffix'                =>  '.php',
								'buildFactory'          =>  'Plain',
                                'generateBaseClasses'   =>  true,
                                'generateFormClasses'   =>  true,
                                'baseClassPrefix'       =>  'Base',
                                'baseClassesDirectory'  =>  'generated',
                                'baseExtends'           =>  '',
								'formExtends'           =>  '');
	
	protected $_elements = array();
	
	protected $_validation = array('root'       =>  array('abstract',
													  'connection',
													  'className',
													  'tableName',
													  'connection',
													  'relations',
													  'columns',
													  'indexes',
													  'attributes',
													  'templates',
													  'actAs',
													  'options',
													  'package',
													  'package_custom_path',
													  'inheritance',
													  'detect_relations',
													  'listeners',
													  'checks',
													  'comment'),

							   'column'     =>  array('name',
													  'format',
													  'fixed',
													  'primary',
													  'autoincrement',
													  'type',
													  'length',
													  'size',
													  'default',
													  'scale',
													  'values',
													  'comment',
													  'sequence',
													  'protected',
													  'zerofill',
													  'owner',
													  'extra',
													  'comment'),

							   'relation'   =>  array('key',
													  'class',
													  'alias',
													  'type',
													  'refClass',
													  'local',
													  'foreign',
													  'foreignClass',
													  'foreignAlias',
													  'foreignType',
													  'autoComplete',
													  'cascade',
													  'onDelete',
													  'onUpdate',
													  'equal',
													  'owningSide',
													  'refClassRelationAlias',
													  'foreignKeyName',
													  'orderBy'),

							   'inheritance'=>  array('type',
													  'extends',
													  'keyField',
													  'keyValue'));

	public function __construct($yml){
		echo "sdaf";
		//$this->buildSchema($yml);
	}
													  
													  
	public function getOption($name)
    {
        if (isset($this->_options[$name]))   {
            return $this->_options[$name];
        }
    }

    public function getOptions()
    {
        return $this->_options;
    }

    public function setOption($name, $value)
    {
        if (isset($this->_options[$name])) {
            $this->_options[$name] = $value;
        }
    }

    public function setOptions($options)
    {
        if ( ! empty($options)) {
          $this->_options = $options;
        }
    }	
	
	
    /**
     * buildSchema
     * Runs through the directory and creates a neat scheme
     */	
	public function buildSchema($schema)
    {
        $array = array();
		
		foreach($schema as $formName => $s){
			
			foreach($s['options'] as $k => $o){
				$this->setOption($k, $o);
			}
			
			foreach($s['fields'] as $name => $f){
				$array[] = $this->parseSchema($name, $f);
			}
			
		}
		
        return $array;
    }	

		
    /**
     * importSchema
     * Converts the yml schema file into concrete files.
     */	
	public function importSchema($schema)
    {
        $schema = (array) $schema;
		
		foreach($schema as $key => $value){
			$data = array($key => $value);
					
			//First build right schema.
			$array = $this->buildSchema($data);
		
			//Start builder
			$builder = new lib_Builder();
			$builder->setOptions($this->getOptions());
			$builder->setLib($this->getOption('buildFactory'));

			//Insert fields into builder
			foreach ($array as $name => $definition) {
				$builder->addField($definition);
			}
			
			//Finally let the builder creates the correct file's
			$builder->build();
		}
    }

	 /**
     * parseSchema
     * Makes an neat schema for field elements.
     */	
	public function parseSchema($name, $schema)
    {
        $defaults = array('name'        =>  $name,
                          'label'       =>  null,
                          'type'        =>  'text',
						  'value'       =>  '',
                          'validators'  =>  array(),
                          'decorators'  =>  array()
                          );
		
		
		foreach((array) $schema as $key => $s){
			if(isset($defaults[$key])){
				$defaults[$key] = $s;
			}
		}
		
		if($defaults['label'] == null){
			$defaults['label'] = $name;
		}
			
		return $defaults;
	}
													  
}

?>